package com.lyn.policy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lyn.common.model.Result;
import com.lyn.common.model.ResultList;
import com.lyn.policy.domain.PolicyDoc;
import com.lyn.policy.service.PolicyDocService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(path = "/api/v1/policy")
@Api(tags = "api.v1.policy", description = "API")
public class PolicyDocController {

	@Autowired
	PolicyDocService policyDocService;
	
	/**
	 * restful:
	 * 页面渲染：GET /view/index
	 * 
	 * 查询一个对象（按主键）：GET /{id}
	 * 查询多个对象（按主键）：POST /selects?pageNumber=1&pageSize=10&ids=xx,yy,zz
	 * 分页查询多个对象（少量查询条件）GET /selectparam?pageNumber=1&pageSize=10&aa=x&bb=y
	 * 
	 * 新增一个对象：POST requestBody vo
	 * 新增多个对象：POST /inserts requestBody List<vo>
	 * 
	 * 修改一个对象：PUT requestBody vo
	 * 修改多个对象（按主键）：PUT /updates requestBody List<vo>
	 * 修改多个对象（少量查询条件）：PUT /updatesparam?aa=x&bb=y requestBody vo
	 * 
	 * 删除一个对象（按主键）：DELETE /{id}
	 * 删除多个对象（按主键）：DELETE /deletes?ids=xx,yy,zz requestBody List<id>
	 * 删除多个对象（少量查询条件）：DELETE /deleteparam?aa=x&bb=y
	 */
	
	
	/**************************************************页面渲染*************************************************/
	
	/**
	 * 页面渲染 
	 * 1. GET方法，需加额外路径/view，以区别查询单个文件的action
	 * 3. 方法名以view开头便于区分，返回ModelAndView
	 */
	@RequestMapping(method = RequestMethod.GET, value="/view/index")
	@ApiOperation(value = "跳转至页面", nickname = "index", notes="xxx")
	@ResponseStatus(code = HttpStatus.OK)
	public ModelAndView viewIndex() {
		return new ModelAndView("/policydoc/index");
	}
	
	/**************************************************查询***************************************************/
	
	/**
	 * 查询单个对象(根据主键)
	 * 1. GET方法，无需额外的路径
	 * 2. 入参id用@PathVariable修饰，放在url中：path = "/{id}"
	 * 3. 方法名用select，返回单个对象，如果希望返回联合对象，则用DTO封装
	 */
	@RequestMapping(method = RequestMethod.GET, path = "/{id}")
	@ApiOperation(value = "查询单个对象(根据主键)", nickname = "select", notes="xxx")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<PolicyDoc> select(
			@PathVariable(name = "id", required = true)
	        @ApiParam(name = "id",required = true,value = "文件id",defaultValue="1IBW6WKPIM9LTZQ5DRZFKY21ULA4C5BS") String id) {
		return policyDocService.select(id);
	}
	
	/**
	 * 查询多个/所有对象（根据id列表）
	 * 1. GET方法，额外的url地址：value="/selects"
	 * 2. 业务参数、分页参数用@RequestParam修饰，分页参数非必填
	 * 3. 方法名用list，返回列表对象
	 */
	@RequestMapping(method = RequestMethod.GET, value="/selects")
	@ApiOperation(value = "查询多个对象（根据id列表）", nickname = "selects", notes="xxx")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<ResultList<PolicyDoc>> selects(
			@RequestParam(required = false) 
			@ApiParam(required = false, name = "ids", value = "文件主键id列表，逗号分隔") String ids, 
			@RequestParam(name = "pageNumber",required = false)
	        @ApiParam(name = "pageNumber",required = false,value = "当前页码",defaultValue = "1") Integer pageNumber, 
	        @RequestParam(name = "pageSize",required = false)
	        @ApiParam(name = "pageSize",required = false,value = "每页行数",defaultValue = "15") Integer pageSize
			) {
		return policyDocService.selects(ids, pageNumber, pageSize);
	}
	
	/**
	 * 查询多个对象（按少量条件查询）
	 * 1. GET方法，额外的url地址：value="/selectparam"
	 * 2. 业务参数、分页参数用@RequestParam修饰，分页参数非必填
	 * 3. 方法名用list，返回列表对象
	 */
	@RequestMapping(method = RequestMethod.GET, value="/selectparam")
	@ApiOperation(value = "获取所有文件", nickname = "selectParam", notes="xxx")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<ResultList<PolicyDoc>> selectParam(
			@RequestParam(required=true, name="deptname") 
			@ApiParam(required=true, name="deptname", value="业务参数：单位名称", defaultValue="南宁市公安局") String deptname,
			@RequestParam(required=true, name="no") 
			@ApiParam(required=true, name="no", value="业务参数：文件编号", defaultValue="10") Integer no, 
			@RequestParam(name = "pageNumber",required = false)
	        @ApiParam(name = "pageNumber",required = false,value = "当前页码",defaultValue = "1") Integer pageNumber, 
	        @RequestParam(name = "pageSize",required = false)
	        @ApiParam(name = "pageSize",required = false,value = "每页行数",defaultValue = "15") Integer pageSize 
			) {
		return policyDocService.selectParam(deptname, no, pageNumber, pageSize);
	}
	

	
	/**************************************************新增***************************************************/
	
	/**
	 * 新增一个对象
	 * 1. POST方法，无需额外的路径
	 * 2. 入参封装在@RequestBody中
	 * 3. 方法名是insert，返回新增的对象
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(value = "新增一个文件", nickname = "insert")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<PolicyDoc> insert(
			@RequestBody
			@ApiParam(required = true, name = "policyDoc", value = "新增的文件") 
			PolicyDoc vo) {
		return policyDocService.insert(vo);
	}
	
	/**
	 * 新增多个对象
	 * 1. POST方法，无需额外的路径
	 * 2. 入参封装在@RequestBody中
	 * 3. 返回新增的对象
	 */
	@RequestMapping(method = RequestMethod.POST, value="/inserts")
	@ApiOperation(value = "新增多个对象", nickname = "inserts")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<ResultList<PolicyDoc>> inserts(
			@RequestBody
			@ApiParam(required = true, name = "inserts", value = "新增的文件") 
			List<PolicyDoc> vos) {
		return policyDocService.inserts(vos);
	}
	
	/**************************************************修改***************************************************/
	
	/**
	 * 修改一个对象（根据id主键定位）
	 * 1. PUT方法，无需路径
	 * 2. 待更新的对象封装在@RequestBody中，需附带id
	 * 3. 返回更新后的单个对象
	 */
	@RequestMapping(method = RequestMethod.PUT)
	@ApiOperation(value = "修改一个对象", nickname = "update")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<PolicyDoc> update(
			@RequestBody
			@ApiParam(required = true, name = "policyDoc", value = "修改的对象") 
			PolicyDoc vo) {
		return policyDocService.update(vo);
	}
	
	/**
	 * 更新多个对象（根据id）
	 * 1. PUT方法，无需额外的路径
	 * 2. 待更新的对象封装在@RequestBody中，需附带id
	 * 3. 返回受影响记录个数
	 */
	@RequestMapping(method = RequestMethod.PUT, value="/updates")
	@ApiOperation(value = "更新多个对象", nickname = "updates")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<Integer> updates(
			@RequestBody
			@ApiParam(required = true, name = "policyDocs", value = "修改的文件") List<PolicyDoc> vos) {
		return policyDocService.updates(vos);
	}
	
	/**
	 * 更新多个对象（少量查询条件）
	 * 1. PUT方法，无需额外的路径
	 * 2. 待更新的对象用@RequestBody修饰，查询条件用@RequestParamh修饰
	 * 3. 返回受影响记录个数
	 */
	@RequestMapping(method = RequestMethod.PUT, value="/updateparam")
	@ApiOperation(value = "更新多个文件（少量查询条件）", nickname = "updateParam")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<Integer> updateParam(
			@RequestBody
			@ApiParam(required = true, name = "policyDoc", value = "修改的文件") PolicyDoc vo, 
			@RequestParam(required=true, name="authorId") 
			@ApiParam(required=true, name="authorId", value="业务参数：作者id") String authorId) { 
		return policyDocService.updateParam(vo, authorId);
	}

	
	/**************************************************删除***************************************************/
	
	/**
	 * 删除一个对象
	 * 1. DELETE方法，无需路径
	 * 3. 入参id用@PathVariable修饰：path = "/{id}"
	 * 4. 返回被删除的对象
	 */
	@RequestMapping(method = RequestMethod.DELETE, path = "/{id}")
	@ApiOperation(value = "删除一个文件", nickname = "delete")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<PolicyDoc> delete(
			@PathVariable(name = "id", required = true)
	        @ApiParam(name = "id",required = true,value = "文件id") String id) {
		return policyDocService.delete(id);
	}
	
	/**
	 * 删除多个对象（根据id删除）
	 * 1. DELETE方法， 路径：/deletes
	 * 2. 入参id列表用@requestBody修饰
	 * 3. 返回被影响记录个数
	 */
	@RequestMapping(method = RequestMethod.DELETE, value = "/deletes")
	@ApiOperation(value = "删除多个文件（根据id删除）", nickname = "/deletes")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<Integer> deletes(
			@RequestParam
	        @ApiParam(name = "ids",required = true,value = "文件ids") 
			String ids) {
		return policyDocService.deletes(ids);
	}
	
	/**
	 * 删除多个文件(根据少量查询条件)
	 * 1. DELETE方法，路径：/deleteparam
	 * 2. 入参用@RequestParam修饰
	 * 3. 受影响的记录个数
	 */
	@RequestMapping(method = RequestMethod.DELETE, value="/deleteparam")
	@ApiOperation(value = "删除多个文件(根据少量查询条件)", nickname = "deleteParam")
	@ResponseStatus(code = HttpStatus.OK)
	public Result<Integer> deleteParam(
			@RequestParam(required=true, name="deptname") 
			@ApiParam(required=true, name="deptname", value="业务参数：单位名称", defaultValue="南宁市公安局") String deptname,
			@RequestParam(required=true, name="no") 
			@ApiParam(required=true, name="no", value="业务参数：文件编号", defaultValue="10") Integer no) {
		return policyDocService.deleteParam(deptname, no);
	}
	
}
